package com.ruoyi.common.cyzUtils;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@Component
public class SSHUtils {
    public StringBuilder remoteInvokeShell(String hostname, int port, String username, String password, String cmds) {
        StringBuilder result = new StringBuilder();
        try {
            /* Create a connection instance */
            Connection conn = new Connection(hostname, port);
            /* Now connect */
            conn.connect();
            /* Authenticate */
            boolean isAuthenticated = conn.authenticateWithPassword(username, password);
            if (!isAuthenticated)
                throw new IOException("Authentication failed.");
            /* Create a session */
            Session sess = conn.openSession();
            // sess.execCommand("uname -a && date && uptime && who");
            sess.execCommand(cmds);
            System.out.println("Here is some information about the remote host:");
            InputStream stdout = new StreamGobbler(sess.getStdout());
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(stdout));
            while (true) {
                String line = br.readLine();
                if (line == null)
                    break;
                System.out.println(line);
                result.append(line);
            }
            /* Show exit status, if available (otherwise "null") */
            System.out.println("ExitCode: " + sess.getExitStatus());
            /* Close this session */
            sess.close();
            /* Close the connection */
            conn.close();
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(2);
        }
        return result;
    }

    public StringBuilder remoteInvokeShell2(String ip,int port,String name,String pwd,String cmds) {
        StringBuilder result = new StringBuilder();
        Connection conn=null;
        try {
            conn = new Connection(ip,port);
            conn.connect();
            if (conn.authenticateWithPassword(name, pwd)) {
                // Open a new {@link Session} on this connection
                Session session = conn.openSession();
                // Execute a command on the remote machine.
                session.execCommand(cmds);

                BufferedReader br = new BufferedReader(new InputStreamReader(session.getStdout()));
                BufferedReader brErr = new BufferedReader(new InputStreamReader(session.getStderr()));

                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println("brErr={}"+line);
                    result.append(line);
                }
                while ((line = brErr.readLine()) != null) {
                }
                if (null != br) {
                    br.close();
                }
                if(null != brErr){
                    brErr.close();
                }
                session.waitForCondition(ChannelCondition.EXIT_STATUS, 0);
                int ret = session.getExitStatus();
                System.out.println("getExitStatus:"+ret);
            } else {
                System.out.println("登录远程机器失败:"+ip);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return result;
    }
}
